package com.mew.game.strategy;

import com.mew.common.dc.db.GameRoomService;
import com.mew.common.dc.db.PlayRecordService;
import com.mew.common.enm.SlotsWinType;
import com.mew.common.enm.StrategyType;
import com.mew.common.entity.game.Jackport;
import com.mew.common.entity.record.PlayRecord;
import com.mew.common.entity.user.Profile;
import com.mew.common.rest.game.BetAfterRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class StrategyService {

    @Autowired
    private RoomProfileDataSupport roomProfileDataSupport;
    @Autowired
    private UserProfileDataSupport userProfileDataSupport;
    @Autowired
    private PlayRecordService playRecordService;
    @Autowired
    private GameRoomService gameRoomService;


    /**
     * dataBack
     * @param request
     */
    public void dataBack(BetAfterRequest request){
        if(request.getWinType()== SlotsWinType.None.code()) {
            jackPortProcessor(request.getRoomId(), request.getBet());
        }

        Profile profile = userProfileDataSupport.persistentProfile(request.getUid(), request.getBet(), request.getPayment(), request.getLevelVO());
        roomProfileDataSupport.persistentPlayInfo(request.getUid(), profile.getGoldNumber(), request.getBet(), request.getPayment(), request.getBetTimes());
        if (request.getRoomRecord() == 1){
            roomProfileDataSupport.persistentProfile(request.getUid(), request.getGameId(), request.getRoomId(), request.getBet(), request.getPayment());
        }

        PlayRecord playRecord = new PlayRecord();
        playRecord.buildData(request, profile);
        playRecordService.insertPlayRecord(playRecord);
    }


    /**
     * 处理奖池
     */
    private void jackPortProcessor(int roomId,long betNumber){
        List<Jackport> jList=gameRoomService.findJackPortByRoomId(roomId);
        if(null==jList||jList.isEmpty()){
            return;
        }
        for (Jackport jackport : jList) {
            Double tax = jackport.getTax();
            jackport.setVal((long)(jackport.getVal()+betNumber*tax));
            updateJackPort(roomId,jackport);
        }
    }
    public void updateJackPort(int roomId,Jackport jackport){
        gameRoomService.updateJackPort(roomId,jackport);
    }
}
